import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:get/get.dart';
import 'package:kq_flutter_tv_widgets/config/kq_global.dart';
import '../../resources/kq_theme_colors.dart';

import 'pull_down_header.dart';
import 'pull_down_menu_controller.dart';
import 'pull_down_menu_list.dart';

// ignore: must_be_immutable
class PullDownMenu extends StatefulWidget {
  /**-----------头部------------- */
  ///标题绘制器，返回标题要显示的文本内容
  final String Function(dynamic item, int menuIndex) titleRender;

  ///指定位置是否外部自己控制header数据的刷新，如果true或者未实现回调，则需外部自己刷新，如果false则内容做更新
  bool Function(int index)? isSelfControlHeaderData;

  /// 指定位置自定义,
  /// 如果需要的自定义widget显示在下拉菜单下方，则返回自定义widget
  /// 如果需要的自定义widget显示在屏幕的中间或者底部，则返回null
  /// 通过调用state.showCustomMenu(index),可收起其他item对应的菜单
  Widget? Function(int index, PullDownMenuController menuController)?
      renderCustomHeaderView;

  /// 指定头部header每个item的宽度,如果有自定义header,则必须实现该回调，非自定义item必须指定宽度
  double Function(int index)? headerViewWidth;

  /// 当前菜单回调，展开还是收起，当外部自定义菜单收起时可调用menuController.resetPullDownState(),重置pulldown状态
  void Function(int index, bool isShow, PullDownMenuController menuController)?
      getCurrentStatus;

  ///初始化数据
  List<dynamic> initChosenData;

  ///header高度，默认64
  final double? height;

  ///边框线的颜色
  final Color borderColor;

  ///背景色
  final Color bgColor;

  ///样式
  final TextStyle? style;

  ///下拉展开后的样式
  final TextStyle? dropDownStyle;

  ///icon下拉颜色
  final Color? iconDropDownColor;

  ///icon默认颜色
  final Color iconColor;

  ///如果不使用overlay,则把菜单以下的widget赋值给menuFollowingWidget
  Widget? menuFollowingWidget;
  /**-----------头部------------- */

  /// 是否是自定义组件，如果不是则会调用回调menuDataRender，否则会调用回调menuCustomWidgetRender
  bool Function(int menuIndex) isShowCustomWidget;

  ///返回菜单header对应menuIndex对应的数据
  List<dynamic>? Function(int menuIndex)? menuDataRender;

  /// 非自定义需实现的方法
  /// 下拉菜单每一项绘制器，返回要显示的文本内容
  final String Function(int menuIndex, dynamic item)? menuItemRender;

  ///返回菜单header对应menuIndex对应的widget,自定义widget选中某一项的值必须调用menuController.setChooseItemData()
  Widget? Function(int menuIndex, PullDownMenuController menuController)?
      menuCustomWidgetRender;

  ///返回菜单header对应menuIndex下拉菜单对应的高度，默认最大308
  double? Function(int menuIndex)? menuWidgetHeight;

  ///是否显示排序数据，根据位置menuIndex来判断是否要显示排序号码
  final bool Function(int menuIndex)? isShowSortRender;

  ///是否禁止下拉
  final bool Function(int menuIndex)? disabledRender;

  /// 选中菜单回调
  final Function(
          dynamic item, int menuIndex, PullDownMenuController menuController)?
      pullDownCallback;

  ///比较两个是否相同，相同选中
  final bool Function(dynamic item, dynamic compareItem)? isDataSame;

  ///返回搜索结果,两个参数：1.menu位置；2.搜索的内容
  final List<dynamic> Function(int menuIndex, String searchContent)?
      searchResultCallback;

  /// 是否时时搜索
  final bool Function(int menuIndex)? aliveTimeSearch;

  ///动画时间
  final int animationMilliseconds;

  ///蒙版颜色
  Color maskColor;

  ///是否使用Overlay 默认：true
  final bool isUseOverlay;

  ///menu位置是否固定，默认是true
  final bool isFixure;

  final PullDownMenuController? pullDownController;

  /// 点击空白区域回调
  final void Function(int menuIndex, PullDownMenuController menuController)?
      dismiss;

  PullDownMenu({
    Key? key,
    required this.titleRender,
    required this.initChosenData,
    this.isSelfControlHeaderData,
    this.renderCustomHeaderView,
    this.getCurrentStatus,
    this.headerViewWidth,
    this.height,
    this.borderColor = KqThemeColors.lineColor,
    this.bgColor = KqThemeColors.bgDefault,
    this.style,
    this.dropDownStyle,
    this.iconDropDownColor,
    this.iconColor = KqThemeColors.text26,
    this.menuFollowingWidget,
    required this.isShowCustomWidget,
    this.menuCustomWidgetRender,
    this.menuWidgetHeight,
    this.menuDataRender,
    this.menuItemRender,
    this.isShowSortRender,
    this.disabledRender,
    this.pullDownCallback,
    this.dismiss,
    this.isDataSame,
    this.searchResultCallback,
    this.aliveTimeSearch,
    this.isUseOverlay = true,
    this.animationMilliseconds = 100,
    this.pullDownController,
    this.maskColor = const Color.fromRGBO(0, 0, 0, 0.3),
    this.isFixure = true,
  }) : super(key: key);

  @override
  State<PullDownMenu> createState() => _PullDownMenuState();
}

class _PullDownMenuState extends State<PullDownMenu> {
  final GlobalKey _stackKey = GlobalKey();
  PullDownMenuController controller = Get.put(PullDownMenuController());

  @override
  void initState() {
    super.initState();
    if (widget.pullDownController != null) {
      controller = widget.pullDownController!;
    } else {
      controller = PullDownMenuController();
    }
    KqGlobal.setPullDownMenuController(controller);
  }

  @override
  void dispose() {
    super.dispose();
    if (KqGlobal.pullDownMenuControllers.contains(controller)) {
      KqGlobal.pullDownMenuControllers.remove(controller);
    }
  }

  @override
  Widget build(BuildContext context) {
    if (widget.isUseOverlay) {
      return PullDownHeader(
        stackKey: _stackKey,
        controller: controller,
        titleRender: widget.titleRender,
        isSelfControlHeaderData: widget.isSelfControlHeaderData,
        renderCustomHeaderView: widget.renderCustomHeaderView,
        headerViewWidth: widget.headerViewWidth,
        getCurrentStatus: widget.getCurrentStatus,
        initChosenData: widget.initChosenData,
        disabledRender: widget.disabledRender,
        height: widget.height,
        borderColor: widget.borderColor,
        bgColor: widget.bgColor,
        dropDownStyle: widget.dropDownStyle,
        iconColor: widget.iconColor,
        iconDropDownColor: widget.iconDropDownColor,
        style: widget.style,
        isUseOverlay: widget.isUseOverlay,
        menuListOverlay: () {
          if (widget.isUseOverlay) {
            return PullDownMenuList(
              controller: controller,
              menuCustomWidgetRender: widget.menuCustomWidgetRender,
              menuDataRender: widget.menuDataRender,
              menuItemRender: widget.menuItemRender,
              menuWidgetHeight: widget.menuWidgetHeight,
              isShowCustomWidget: widget.isShowCustomWidget,
              isDataSame: widget.isDataSame,
              searchResultCallback: widget.searchResultCallback,
              aliveTimeSearch: widget.aliveTimeSearch,
              animationMilliseconds: widget.animationMilliseconds,
              maskColor: widget.maskColor,
              isUseOverlay: widget.isUseOverlay,
              pullDownCallback: widget.pullDownCallback,
              isFixure: widget.isFixure,
              dismiss: widget.dismiss,
            );
          }
          return Container();
        },
      );
    }
    return Stack(
      key: _stackKey,
      clipBehavior: Clip.none,
      children: <Widget>[
        Column(
          children: <Widget>[
            PullDownHeader(
              stackKey: _stackKey,
              controller: controller,
              titleRender: widget.titleRender,
              isSelfControlHeaderData: widget.isSelfControlHeaderData,
              headerViewWidth: widget.headerViewWidth,
              initChosenData: widget.initChosenData,
              getCurrentStatus: widget.getCurrentStatus,
              disabledRender: widget.disabledRender,
              height: widget.height,
              borderColor: widget.borderColor,
              bgColor: widget.bgColor,
              dropDownStyle: widget.dropDownStyle,
              iconColor: widget.iconColor,
              iconDropDownColor: widget.iconDropDownColor,
              style: widget.style,
            ),
            widget.menuFollowingWidget ?? const SizedBox()
          ],
        ),
        PullDownMenuList(
          controller: controller,
          isShowCustomWidget: widget.isShowCustomWidget,
          menuDataRender: widget.menuDataRender,
          menuItemRender: widget.menuItemRender,
          menuCustomWidgetRender: widget.menuCustomWidgetRender,
          menuWidgetHeight: widget.menuWidgetHeight,
          isDataSame: widget.isDataSame,
          searchResultCallback: widget.searchResultCallback,
          aliveTimeSearch: widget.aliveTimeSearch,
          animationMilliseconds: widget.animationMilliseconds,
          maskColor: widget.maskColor,
          isShowSortRender: widget.isShowSortRender,
          pullDownCallback: widget.pullDownCallback,
          isFixure: widget.isFixure,
          dismiss: widget.dismiss,
        ),
      ],
    );
  }
}
